    //
    //  EmoticonPackage.swift
    //  Emoticon
    //
    //  Created by 袁航 on 15/8/6.
    //  Copyright © 2015年 yuanhang. All rights reserved.
    //
    
import UIKit
    // MARK:- 模型准备
    // 图片包数据信息
    class EmoticonPackage: NSObject {
        /// 文件夹名
        var id: String = ""
        ///  图片包信息数组
        var emoticonInfoModel: EmoticonInfo?
        ///  字典转模型
        init(dict: [String: AnyObject]) {
            super.init()
            setValuesForKeysWithDictionary(dict)
        }
        ///  防止键值出错
        override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    /// 重写description方法
        override var description: String {
            return "\(id)--\(emoticonInfoModel)"
        }
    }
    // 图片包信息info
    class EmoticonInfo: NSObject {
        ///  文件夹名
        var id: String?
        ///  组名(简体中文)
        var group_name_cn: String?
        /// 字典数组
        var emoticons: [[String: String]]
        ///  图片包模型数组
        var emoticonModels = [Emoticons]()
        ///  字典转模型
        init(dict: [String: AnyObject]) {
            emoticons = [[String: String]]()
            super.init()
            setValuesForKeysWithDictionary(dict)
        }
        ///  防止键值出错
        override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
        /// 重写description方法
        override var description: String {
            return "\(id)--\(group_name_cn)--\(emoticonModels)"
        }
    }
    // 图片信息
    class Emoticons: NSObject {
        /// 定义变量,记录自身被点击的次数
        var clickCount: Int = 0
        /// 是否需要添加删除按钮
        var remove: Bool = false
        /// 文件夹名
        var id: String?
        ///  图片名(简体中文)
        var chs: String?
        ///  gif图片
        var gif: String?
        ///  png图片
        var png: String?
        /// png图片路径
        var pngImagePath: String {
            if png == nil {
                return ""
            }
            let path = ((bundlePath as NSString).stringByAppendingPathComponent(id!) as NSString) .stringByAppendingPathComponent(png!)
            return path
        }
        /// gif图片路径
        var gifImagePath: String {
            if gif == nil {
                return ""
            }
            let path = ((bundlePath as NSString).stringByAppendingPathComponent(id!) as NSString).stringByAppendingPathComponent(gif!)
            return path
        }
        ///  emoji图片code
        var code: String? {
            didSet{
                let scanler = NSScanner(string: code!)
                var result: UInt32 = 0
                scanler.scanHexInt(&result)
                emoji = String(Character(UnicodeScalar(result)))
            }
        }
        ///  emoji图片unicode字符
        var emoji: String?
        
        ///  字典转模型
        init(id: String?, dict: [String: String]) {
            self.id = id
            super.init()
            setValuesForKeysWithDictionary(dict)
        }
        ///  防止键值出错
        override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
        /// 构造方法,标记是否需要插入删除图片
        init(remove: Bool) {
            self.remove = remove
        }
        /// 重写description方法
        override var description: String {
            return "\(chs)--\(gif)--\(png)--\(code)"
        }
    }
    
    
    // 读取bundle路径
    let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
    // MARK:- 提供类方法,转模型(使用延展类,防止模型中代码过多)
    extension EmoticonPackage {
        class var EmoticonPackageModels: [EmoticonPackage] {
            // 读取plist信息
            let path = (bundlePath as NSString).stringByAppendingPathComponent("emoticons.plist")
            // 读取字典数据
            let packDict = NSDictionary(contentsOfFile: path)
            // 读取(packages)数组数据
            let packages = packDict!["packages"] as! [[String : AnyObject]]
            // 字典转模型
            // 定义模型可变数组
            var packagesM = [EmoticonPackage]()
            // 插入一组空数据,预留使用过的表情
            packagesM.append(EmoticonPackage(dict: ["groupName": "最近"]).loadEmoticonInfo())
            for dict in packages {
                packagesM.append(EmoticonPackage(dict: dict).loadEmoticonInfo())
            }
            return packagesM
        }
        
        // 根据获取的package模型ID进一步获取EmoticonInfo数据,转模型(链式访问)
        private func loadEmoticonInfo() -> Self {
            // 获取路径
            let path = ((bundlePath as NSString).stringByAppendingPathComponent(id) as NSString).stringByAppendingPathComponent("info.plist")
            // 读取数据
            let emoticonDict = NSDictionary(contentsOfFile: path) as? [String : AnyObject]
            if emoticonDict != nil{
                // 字典转模型
                emoticonInfoModel = EmoticonInfo(dict: emoticonDict!).loadEmoticons()
            } else {
                // 字典转模型
                emoticonInfoModel = EmoticonInfo(dict:["group_name_cn": "最近"]).loadEmoticons()
            }
            return self
        }
    }
    // 加载emoticons模型数组
    extension EmoticonInfo {
        private func loadEmoticons() -> Self {
            // 定义模型数组
            var emoticonsM = [Emoticons]()
            // 遍历字典数组转模型
            var index = 0
                for dict in emoticons {
                    index++
                    emoticonsM.append(Emoticons(id:id, dict: dict))
                    // 追加一个删除按钮的对象标记
                    if index == 20 {
                        index = 0
                        emoticonsM.append(Emoticons(remove: true))
                    }
                }
            // 查看图片数是否是20的倍数,如果不是,需要添加空对象,排版
            // 在此处不能用模型来计算个数,因为模型中已经追加了删除按钮
            let count = emoticons.count % 20
            if count > 0 || emoticons.count == 0{
                for _ in count..<20 {
                    emoticonsM.append(Emoticons(remove: false))
                }
                // 最后追加一个删除按钮
                emoticonsM.append(Emoticons(remove: true))
            }
            // 给属性赋值
            emoticonModels = emoticonsM
            return self
        }
    }
    